package com.hongfeng.support.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.hongfeng.support.annotation.Online;
import com.hongfeng.support.common.Constants;
import com.hongfeng.support.common.Page;
import com.hongfeng.support.controller.common.BaseController;
import com.hongfeng.support.item.ParmItem;
import com.hongfeng.support.item.tag.FieldOperator;
import com.hongfeng.support.model.User;
import com.hongfeng.support.service.UserService;
import com.hongfeng.support.util.WebHelper;
import com.hongfeng.support.util.encrypt.DESUtil;
import com.hongfeng.support.util.json.JsonUtils;

/**
 * 用户Controller
 * 
 * @author llc
 * 
 */
@Controller
@Online
public class UserController extends BaseController {

	@Resource
	private UserService userService;

	@RequestMapping("list")
	public void list(HttpServletRequest request, HttpServletResponse response,
			String username, Page<User> page) {
		log.info("查询用户名：" + username);
		ParmItem item = new ParmItem("username", username, FieldOperator.LIKE);
		userService.findPageByQuery(page, User.class, item);
		String json = JsonUtils.bean2Json(page);
		this.sendSuccResponse(response, json);
	}

	@RequestMapping("create")
	public void create(HttpServletRequest request,
			HttpServletResponse response, @RequestParam("user") String userJson)
			throws Exception {
		log.info("添加用户:" + (userJson != null ? userJson : null));
		if (userJson == null) {
			this.sendErrorResponse(response, "用户信息不能为空。");
			return;
		}
		User user = (User) JsonUtils.json2Object(userJson, User.class);
		ParmItem item = new ParmItem("username", user.getUsername(),
				FieldOperator.LIKE);
		List<User> users = userService.findAllByQuery(User.class, item);
		if (users.size() == 0) {
			if (user.getRole().getId() == null) {
				user.setRole(null);
			}
			DESUtil des = new DESUtil();
			user.setPassword(des.encrypt(user.getPassword()));
			userService.create(user);
		} else {
			this.sendErrorResponse(response, "用户名已经存在。");
		}
	}

	@RequestMapping("update")
	public void update(HttpServletRequest request,
			HttpServletResponse response, @RequestParam("user") String userJson)
			throws Exception {
		log.info("修改用户:" + (userJson != null ? userJson : null));
		if (userJson == null) {
			this.sendErrorResponse(response, "用户信息不能为空。");
			return;
		}
		User user = (User) JsonUtils.json2Object(userJson, User.class);
		User qUser = userService.read(user.getId());
		List<User> users = null;
		if (!user.getUsername().equals(qUser.getUsername())) {
			ParmItem item = new ParmItem("username", user.getUsername(),
					FieldOperator.LIKE);
			users = userService.findAllByQuery(User.class, item);
		}
		if (users == null || users.size() == 0) {
			if (user.getRole().getId() == null) {
				user.setRole(null);
			}
			DESUtil des = new DESUtil();
			user.setPassword(des.encrypt(user.getPassword()));
			user.setCreateTime(qUser.getCreateTime());
			user.setCreator(qUser.getCreator());
			userService.update(user);
		} else {
			this.sendErrorResponse(response, "用户名已经存在。");
		}
	}

	@RequestMapping("modifyPwd")
	public void modifyPwd(HttpServletRequest request,
			HttpServletResponse response,
			@RequestParam("password") String password) throws Exception {
		log.info("修改密码:" + (password != null ? password : null));
		if (password == null) {
			this.sendErrorResponse(response, "密码不能为空。");
			return;
		}
		User user = WebHelper.getSessionAttribute(request,
				Constants.SESSION_LOGIN_USER);
		DESUtil des = new DESUtil();
		user.setPassword(des.encrypt(password));
		userService.update(user);
	}

	@RequestMapping("read")
	public void read(HttpServletRequest request, HttpServletResponse response,
			@RequestParam("id") Long id) throws Exception {
		log.info("读取用户:" + id);
		User user = userService.read(id);
		DESUtil des = new DESUtil();
		user.setPassword(des.decrypt(user.getPassword()));
		String json = JsonUtils.bean2Json(user);
		log.info("用户信息：" + json);
		this.sendSuccResponse(response, json);
	}

	@RequestMapping("delete")
	public void delete(HttpServletRequest request,
			HttpServletResponse response, @RequestParam("ids") String ids) {
		log.info("删除用户:" + (ids != null ? ids.toString() : null));
		if (ids == null || "".equals(ids)) {
			this.sendErrorResponse(response, "请选择要删除的用户。");
			return;
		}
		String[] idArr = ids.split(",");
		for (String id : idArr) {
			userService.delete(Long.parseLong(id));
		}
	}

}